package com.it.base.core.system.dept.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.it.base.common.view.PageResult;
import com.it.base.common.view.Result;
import com.it.base.common.view.ResultCode;
import com.it.base.core.system.dept.model.covert.SysDeptCovert;
import com.it.base.core.system.dept.model.dto.SysDeptDTO;
import com.it.base.core.system.dept.model.dto.SysDeptListDTO;
import com.it.base.core.system.dept.model.entity.SysDept;
import com.it.base.core.system.dept.model.from.SysDeptDeleteOneFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptSaveFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptSelectListFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptSelectOneFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptSelectPageListFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptUpdateFormDTO;
import com.it.base.core.system.dept.model.from.SysDeptValidatorFormDTO;
import com.it.base.core.system.dept.service.ISysDeptService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ApiSort(3)
@RestController
@RequestMapping("system/dept")
@Api(tags = "3.部门数据")
public class SysDeptController {

	@Autowired
	private ISysDeptService sysDeptService;

	@GetMapping("test")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "1.测试", notes = "测试")
	public Result<?> test() {
		return Result.SUCCESS();
	}

	@PostMapping("selectPageList")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "2.分页查询部门数据", notes = "分页部门")
	public Result<PageResult<SysDeptListDTO>> selectPageList(
			@Validated @RequestBody SysDeptSelectPageListFormDTO sysDeptSelectPageListFormDTO) {
		PageResult<SysDept> page = sysDeptService.selectPageList(sysDeptSelectPageListFormDTO);
		List<SysDeptListDTO> datas = SysDeptCovert.INSTANCE.entity2DTOList(page.getRows());
		return Result.SUCCESS(new PageResult<SysDeptListDTO>(page.getTotal(), datas));
	}

	@PostMapping("validatorExist")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "3.校验部门数据", notes = "校验部门数据")
	public Result<?> validatorExist(@Validated @RequestBody SysDeptValidatorFormDTO sysDeptValidatorFormDTO) {
		sysDeptValidatorFormDTO.setId(null);
		return Result.SUCCESS(this.sysDeptService.validatorExist(sysDeptValidatorFormDTO));
	}

	@PostMapping("saveOne")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "4.新增部门数据", notes = "新增部门数据")
	public Result<String> saveOne(@Validated @RequestBody SysDeptSaveFormDTO sysDeptSaveFormDTO) {
		// 1.校验
		SysDeptValidatorFormDTO sysDeptValidatorFormDTO = SysDeptValidatorFormDTO.builder()//
				.deptName(sysDeptSaveFormDTO.getDeptName())//
				.parentId(sysDeptSaveFormDTO.getParentId())//
				.build();
		boolean flag = this.sysDeptService.validatorExist(sysDeptValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100116);
		}

		// 2.保存
		SysDept SysDept = SysDeptCovert.INSTANCE.from2Entity(sysDeptSaveFormDTO);
		this.sysDeptService.save(SysDept);
		return Result.SUCCESS(SysDept.getId());
	}

	@PostMapping("selectOne")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "5.查询部门数据", notes = "查询部门数据")
	public Result<SysDeptDTO> selectOne(@Validated @RequestBody SysDeptSelectOneFormDTO sysDeptSelectOneFormDTO) {
		SysDept SysDept = this.sysDeptService.getById(sysDeptSelectOneFormDTO.getId());
		SysDeptDTO SysDeptDTO = SysDeptCovert.INSTANCE.entity2DTO(SysDept);
		return Result.SUCCESS(SysDeptDTO);
	}

	@PutMapping("updateOne")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "6.更新部门数据", notes = "更新部门数据")
	public Result<?> updateOne(@Validated @RequestBody SysDeptUpdateFormDTO sysDeptUpdateFormDTO) {
		// 1.校验
		SysDeptValidatorFormDTO sysDeptValidatorFormDTO = SysDeptValidatorFormDTO.builder()//
				.deptName(sysDeptUpdateFormDTO.getDeptName())//
				.parentId(sysDeptUpdateFormDTO.getParentId())//
				.id(sysDeptUpdateFormDTO.getId())//
				.build();
		boolean flag = this.sysDeptService.validatorExist(sysDeptValidatorFormDTO);
		if (flag) {
			return new Result<>(ResultCode.E_100127);
		}

		// 2.判断父部门不等式自己的id
		if (sysDeptUpdateFormDTO.getId().equals(sysDeptUpdateFormDTO.getParentId())) {
			return new Result<>(ResultCode.E_100126);
		}

		// 3.判断部门状态不能停用

		// 4.更新部门
		// 4.1修改自身
		// 4.2修改子元素

		SysDept SysDept = SysDeptCovert.INSTANCE.from2Entity(sysDeptUpdateFormDTO);
		this.sysDeptService.updateById(SysDept);
		return Result.SUCCESS(SysDept.getId());
	}

	@DeleteMapping("deleteOne")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "7.删除部门类型", notes = "删除部门")
	public Result<Object> deleteOne(@Validated @RequestBody SysDeptDeleteOneFormDTO sysDeptDeleteOneFormDTO) {

		if (this.sysDeptService.hasChildByDeptId(sysDeptDeleteOneFormDTO.getId())) {
			return new Result<>(ResultCode.E_100125);
		}

		this.sysDeptService.removeById(sysDeptDeleteOneFormDTO.getId());
		return Result.SUCCESS();
	}

	@PostMapping("selectList")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "8.查询部门数据", notes = "查询部门")
	public Result<List<SysDeptListDTO>> selectList(
			@Validated @RequestBody SysDeptSelectListFormDTO sysDeptSelectListFormDTO) {
		List<SysDept> list = sysDeptService.selectList(sysDeptSelectListFormDTO);
		List<SysDeptListDTO> datas = SysDeptCovert.INSTANCE.entity2DTOList(list);
		return Result.SUCCESS(datas);
	}

}
